﻿using System;
using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.IO;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using HIS.Utils;
using HISModels.Entities;
using System.Configuration;
using Newtonsoft.Json.Linq;

namespace HIS.Controllers.Master {

    public class ItemShapes : HIS.Controllers.AppBaseController {

        #region Fields

        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private RefItemShape refItemShape = null;
        #endregion

        #region Items Shapes
        public ItemShapes() {
            Data.Add("Pagetitle", "Item Shapes");
            Data.Add("Notification", strNotification);
        }

        [Authorization(RoleAction = "master-items")]
        public override void Index() {
            try {
                Data["Pagetitle"] += " :: List Item Shapes ::";
                RenderFile("master\\items\\itemshapes.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-items")]
        public void TableList() {
            try {

                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string codevalue = HttpContext.Current.Request["codevalue"].NullSafeToString();
                string namevalue = HttpContext.Current.Request["namevalue"].NullSafeToString();
                string descvalue = HttpContext.Current.Request["descvalue"].NullSafeToString();



                string ssearch = "code=" + codevalue + ";nama=" + namevalue + ";desc=" + descvalue;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //pagingSearch.sSearch = HttpContext.Current.Request["sSearch"];
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }
                pagingSearch.sSearch = ssearch;

                string strUri = string.Empty;
                strUri = Global.ServiceUriDictionary["itemshapepaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));

                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-items")]
        public void EditShapes() {

            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {
                GoToUrl("itemshapes/"); return;
            }

            try {
                string strUri = Global.ServiceUriDictionary["itemshape"] + id + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                refItemShape = JsonConvert.DeserializeObject<RefItemShape>(stringReader.ReadToEnd());
                Data["Pagetitle"] += " :: Edit Item Shapes";
                refItemShape.SetNullToEmptyString();
                Data.Add("refItemShape", refItemShape);
                Data.Add("IsCreate", "false");
                Data.Add("Actionform", Data["rooturl"] + "master/itemshapes/editshapes/");
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\items\\editshapes.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-items")]
        public void DeleteItemShapes() {
            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {
                GoToUrl("itemshapes/");
                return;
            }

            try {
                refItemShape = new HISModels.Entities.RefItemShape();
                string strUri = Global.ServiceUriDictionary["itemshape"] + Global.ServiceUriDictionary["requestformat"];

                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                var a = JsonConvert.DeserializeObject<RefItemShape>(stringReader.ReadToEnd());
                if (strNotification == string.Empty) strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                HttpContext.Current.Response.Redirect(Data["rooturl"] + "master/itemshapes/");
                base.RestService.Dispose();
            }
        }


        [Authorization(RoleAction = "master-items")]
        public void SaveItemShapes() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                refItemShape = new RefItemShape(); Utils.Helper.SetNullToEmptyString(refItemShape);
                if (HttpContext.Current.Request["IsCreate"] != "true") {
                    int id = 0;
                    try {
                        id = Convert.ToInt32(HttpContext.Current.Request["idem"]);
                    } catch { }
                    strUri = Global.ServiceUriDictionary["itemshape"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    refItemShape = JsonConvert.DeserializeObject<RefItemShape>(stringReader.ReadToEnd());
                }

                refItemShape.PostHandler<HISModels.Entities.RefItemShape>();
                strUri = Global.ServiceUriDictionary["itemshape"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(refItemShape));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(refItemShape));
                }
                string temp = stringReader.ReadToEnd();


                refItemShape = JsonConvert.DeserializeObject<RefItemShape>(temp);

                if (refItemShape != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                //strNotification = "{\"strNotification\": \"" + ex.Message + "\"}";
                HttpContext.Current.Response.Write(ex.Message);
            } finally {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-items")]

        public void CreateItemShapes() {
            StringReader stringReader = null;
            try {
                Data["Pagetitle"] += " :: Add Item Shapes ::";
                refItemShape = new HISModels.Entities.RefItemShape() { CodeValue = string.Empty, NameValue = string.Empty, DescValue = string.Empty };
                Data.Add("refItemShape", refItemShape);
                Data.Add("IsCreate", "true");
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\items\\editshapes.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        #endregion

    }

}

